package com.example.controller;

import com.example.domain.Blog;
import com.example.domain.property.Comment;
import com.example.domain.property.Tag;
import com.example.domain.visit.Visiter;
import com.example.service.BlogService;
import com.example.service.CommentService;
import com.example.service.TagService;
import com.example.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.*;

@Controller
@RequestMapping("/other")
public class OtherController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;
    //传递搜索信息
    @GetMapping("/search")
    @ResponseBody
    public String index1( String query, HttpSession session){
        session.setAttribute("query",query);  //搜索内容
        return query;
    }
    //根据搜索内容全文章查询
    @GetMapping("/gainSearch/{currentPage}/{pageSize}")
    @ResponseBody
    public Map<String,Object> index2(@PathVariable int currentPage, @PathVariable int pageSize,HttpSession session){
        String query=(String)session.getAttribute("query");  //搜索内容
        Page<Blog> page = blogService.findByQuery(query,currentPage,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("query",query);
        map.put("page",page);
        return map;
    }
    //文章点击事件展示文章详情
    @RequestMapping("/getByid")
    public String index3(Long id,HttpSession session){
        session.setAttribute("blogClickId",id);
        return "redirect:/front/blog.html";
    }
    //文章详情页获取文章信息
    @GetMapping("/gainBlog")
    @ResponseBody
    public Blog index4(HttpSession session){
        Long id =(Long) session.getAttribute("blogClickId");
        Blog blog = blogService.selectOne(id);
        blog.setComments(null);
        return blog;
    }
    //文章详情页获取评论信息
    @GetMapping("/gainComments")
    @ResponseBody
    public List<Comment> index5(HttpSession session){
        Long id =(Long) session.getAttribute("blogClickId");
        Blog blog = blogService.selectOne(id);
        List<Comment> comments = commentService.findAllByBlogId(id);
        return comments;
    }
    //文章详情页添加文章评论信息
    @PostMapping("/saveComment/{blogId}/{parentId}")
    @ResponseBody
    public Boolean index6(@RequestBody Comment comment,@PathVariable Long blogId,@PathVariable Long parentId){
        System.out.println("获取的评论信息"+comment+"文章id"+blogId);
        Blog blog = new Blog();
        blog.setId(blogId);
        comment.setBlog(blog);
        Comment parent=new Comment();
        parent.setId(parentId);
        comment.setParent(parent);
        comment.setCreateTime(new Date());
        Boolean flag = commentService.saveComment(comment);
        return flag;
    }
}
